package com.hualan.mapper;

import com.hualan.pojo.ConsultantLevels;
import com.hualan.pojo.Consultants;
import com.hualan.pojo.Shanchang;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ShaixuanMapper {

    @Select("select * from shanchang ")
    List<Shanchang> listAll();


    @Select("<script>" +
            "select DISTINCT consultants.* from consultants " +
            "join consultant_levels ON consultants.level_id=consultant_levels.id " +
            "<if test=\"level != '选择等级'\" >" +
            "AND consultant_levels.level_name = #{level} " +
            "</if> " +
            "join shanchang "+
            "join shanchang_consultant ON shanchang_consultant.consultant_id=consultants.id and shanchang_consultant.shanchang_id = shanchang.id "+
            "<if test='list != null and list.size() > 0'>" +
            "AND shanchang.id IN " +
            "<foreach item='item' index='index' collection='list' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach> " +
            "</if> " +
            "join shoufei "+
            "join shoufei_fenlei on shoufei_fenlei.shoufei_id=shoufei.id and shoufei_fenlei.consultant_id=consultants.id "+
            "<if test='zixun != 0'>" +
            "AND shoufei.id = #{zixun} " +
            "</if> " +
            "WHERE " +
            "<if test='diyu != null'>" +
            "consultants.location = #{diyu} " +
            "</if> " +
            "<if test='diyu == null'> " +
            "1=1 "+
            "</if>" +
            "and consultants.del=0 and shanchang.del=0" +
            "</script>")
    @Results({
            @Result(property = "id",column = "id",id = true),
            @Result(
                    property = "consultantLevels",
                    column = "level_id",
                    one = @One(select = "findById")),
            @Result(
                    property = "shoufeiList",
                    column = "id",
                    many = @Many(select = "findByShanchangId")
            )
    })
    List<Consultants> shaiXuan(@Param("diyu") String diyu,@Param("zixun") Integer zixun,@Param("level") String level,@Param("list") List<Integer> list);

    @Select("select level_name from consultant_levels where id = #{id}")
    ConsultantLevels findById(Integer id);

    @Select("select shanchang.* from shanchang join shanchang_consultant on shanchang.id=shanchang_consultant.shanchang_id where shanchang_consultant.consultant_id = #{id}")
    Shanchang findByShanchangId(Integer id);
}
